#!/usr/bin/env bash

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

network_name="example.com"
readonly network_name
network="10.5.0.0/24"
readonly network
gateway="$(echo ${network} | cut -f1-3 -d'.').254"
readonly gateway

function kerberos::_check_networks_exists {
    if docker network ls  --format '{{.Name}}' | grep "^${network_name}$" &> /dev/null; then
      return 1
    fi
    return 0
}

function kerberos::create_kerberos_network {
    if ! kerberos::_check_networks_exists; then
      return 0
    fi

    docker network create \
        --driver=bridge \
        --subnet="${network}" \
        --ip-range="${network}" \
        --gateway="${gateway}" \
        "${network_name}"

    echo "Create network \"${network_name}\" for Kerberos integration"
}

function kerberos::delete_kerberos_network {
    if kerberos::_check_networks_exists; then
      return 0
    fi

    docker network rm "${network_name}"

    echo "Deleted network \"${network_name}\" for Kerberos integration"
}
